package com.kly.pay.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.androidpublisher.AndroidPublisher;
import com.google.api.services.androidpublisher.AndroidPublisherScopes;
import com.kly.pay.factory.GoogleApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;

/**
 * @author linqu
 */
@Slf4j
@Service("ppGoogleApi")
public class PassionPulseGoogleApiImpl implements GoogleApiService {
    /**
     * 接入凭证
     */
//    @Value("${pp.google.pay.auth.credential}")
//    private String googlePayCredential;
    /**
     * 一次性商品
     */
    private AndroidPublisher.Purchases.Products products;
    /**
     * 订阅api_v2
     */
    private AndroidPublisher.Purchases.Subscriptionsv2 subscriptionsv2;
    /**
     * 订阅api
     */
    private AndroidPublisher.Purchases.Subscriptions subscriptions;

    private static final String googlePayCredential = "{\n" +
            "  \"type\": \"service_account\",\n" +
            "  \"project_id\": \"passionpulse-6112f\",\n" +
            "  \"private_key_id\": \"5e34cb79703e755e5bdbfadc90c68f9e3a8ac01b\",\n" +
            "  \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC5VHxMNOGDqucB\\nml7eCRYOcKtG6w3KUz9Kw2iT7FEhuKu+f2YgKDMWz6ubZxC1+yCp40oY8HdQiLrU\\niE2jmEl2782C9+6HAbfSmDmYwsc7bGTkzBVnj5QCpfrSb0syWflwueCds1vppzup\\nCqukzbxOG7yIEA8rhAriEtm8y13in7/lhFBAC2UZguUWH0Ok1OizU9/pp0JbtIQZ\\n3+fanaCp+WbxbFMxwi+Aj7eZFzA5GRwslxyzQ6A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uJp3Wk15rr/Sf4GwNesqKfRoHBuDZZfKHwBN\\n5Vw9OtA1iX+yJFGf71rdm9kkFJVcvadjg+u+xpKTICbWtlVXxwgIA3L05c5rhFhe\\njdbiJyKXubhpN6HLFIMgSmyAHFf8QNuZ2onG/YvLwQKBgBhfi3RMDUDqxpB08Qz5\\nmIeoFB/Mew+rNRx9Ntf7QDC6cAyiYaxhZzc3tKFCipJz1tfNa8/expTx9VJR8eXa\\nJj/x/CQNxrkoSkV5fIZYFOBf4GI9l0wjIoA8DUp0xhXi+yK1Got6y6fmPNgRNf0z\\naNCgh9IKFLjlUa3WenSYxJYL\\n-----END PRIVATE KEY-----\\n\",\n" +
            "  \"client_email\": \"pp-pay@passionpulse-6112f.iam.gserviceaccount.com\",\n" +
            "  \"client_id\": \"106399706746116188334\",\n" +
            "  \"auth_uri\": \"https://accounts.google.com/o/oauth2/auth\",\n" +
            "  \"token_uri\": \"https://oauth2.googleapis.com/token\",\n" +
            "  \"auth_provider_x509_cert_url\": \"https://www.googleapis.com/oauth2/v1/certs\",\n" +
            "  \"client_x509_cert_url\": \"https://www.googleapis.com/robot/v1/metadata/x509/pp-pay%40passionpulse-6112f.iam.gserviceaccount.com\",\n" +
            "  \"universe_domain\": \"googleapis.com\"\n" +
            "}";

    @PostConstruct
    public void initGooglePay() {
        try {
            // createScoped给令牌访问权限设置使用的权限范围
            GoogleCredential credential = GoogleCredential.fromStream(new ByteArrayInputStream(googlePayCredential.getBytes()))
                    .createScoped(AndroidPublisherScopes.all());
            if(credential==null) {
                log.error("==========>Get GoogleCredential fails 获取谷歌凭证失败！");
            }
            HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
            JacksonFactory jacksonFactory = JacksonFactory.getDefaultInstance();
            AndroidPublisher publisher = new AndroidPublisher.Builder(httpTransport, jacksonFactory, credential).build();
            this.products = publisher.purchases().products();
            this.subscriptions = publisher.purchases().subscriptions();
            this.subscriptionsv2 = publisher.purchases().subscriptionsv2();

        }  catch (Exception e) {
            log.error("==========>Init GoogleCredential failed", e);
        }
    }

    @Override
    public String getPackageName() {
        return "com.kky.pp";
    }
    @Override
    public AndroidPublisher.Purchases.Products getProducts() {
        return this.products;
    }
    @Override
    public AndroidPublisher.Purchases.Subscriptions getSubscriptions() {
        return this.subscriptions;
    }
    @Override
    public AndroidPublisher.Purchases.Subscriptionsv2 getSubscriptionsv2() {
        return this.subscriptionsv2;
    }
}
